<%--
  Created by IntelliJ IDEA.
  User: dzw
  Date: 2017/12/1
  Time: 15:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<p id="pSids" style="display: none">${sids}</p>
<div class="jqGrid_wrapper">
  <table id="dialog_businessTalk_table"></table>
  <div id="dialog_businessTalk_page"></div>
</div>

<script>

  var selectedTalks = [
    <c:forEach var="item" items="${selectedTalks}" varStatus="status">
    {"id":"${item.id}","topic":"${item.talkTopic}"} <c:if test="${!status.last}">,</c:if>
    </c:forEach>
  ];

  $(function(){
    // 加载数据
    init_grid_dialog_businessTalk();
    // 隐藏表格的全选
    $("#cb_dialog_businessTalk_table").hide();
  });

  // 初始化表单数据
  function init_grid_dialog_businessTalk() {

    __init_jqgrid('dialog_businessTalk_table', 'dialog_businessTalk_page',
            '<%=path%>/com/businessTalk/list?viewtype=self',
            ['洽谈主题', 'id'],
            [
              {name: 'talkTopic', index: 'talkTopic', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
              {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
              multiselect: true,
              autowidth: false,
              width: 558,
              onSelectRow: function (rowId, status) {
                if(status){ // true 选中状态
                  setSelectValue();
                } else {    // false 取消选中状态
                  deletetSelectValue(rowId);
                }
              },
              gridComplete: function(){
                var sids = $("#pSids").text();
                if(sids){
                  var arr = sids.split(",");
                  for(var i=0; i<arr.length; i++){
                    $(this).jqGrid("setSelection",arr[i]);
                  }
                }
              }
            }
    )

  }

  /* 设置选中id到p */
  function setSelectValue(){

    var rowId = $("#dialog_businessTalk_table").jqGrid('getGridParam', 'selrow'); //获取选中的Id

    var value = "";
    var selectedValue = $("#pSids").text();   /* 获取此前选中值 */
    if(selectedValue != "" && selectedValue != null){
      if(selectedValue.indexOf(rowId) < 0){
        value = selectedValue + "," +　rowId;
      } else {
        return false;
      }
    } else {
      value = rowId;
    }
    /* 将选中的值设置在页面中 */
    $("#pSids").text(value);

    /* 同时将选中的值添加到数组中 */
    var rowDatas = $("#dialog_businessTalk_table").jqGrid('getRowData', rowId);
    var addTalk = {"id":rowDatas.id,"topic":rowDatas.talkTopic};
    selectedTalks.push(addTalk);
  }

  //取消时删除此id
  function deletetSelectValue(rowId){
    for(var i=0; i<selectedTalks.length; i++){
      if(rowId==selectedTalks[i].id){
        selectedTalks.splice(i,1);    // 相等时，删除数组里面对应的对象
        break;
      }
    }
    var selectedValue = $("#pSids").text();
    var str = selectedValue.split(",");
    var selectedValueLast = "";
    for(var i = 0; i < str.length; i++){
      if(rowId != str[i]){
        selectedValueLast += str[i] + ",";
      }
    }
    selectedValueLast = selectedValueLast.substring(0, selectedValueLast.length-1);
    /* 将选中的值设置在页面中 */
    $("#pSids").text(selectedValueLast);
  }

  function fn_dialog_businessTalks_select(callback) {
    var talkTopics = "";
    var talkIds = "";
    for(var i=0; i<selectedTalks.length; i++){
      talkTopics += selectedTalks[i].topic + ",";
      talkIds += selectedTalks[i].id + ",";
    }
    talkTopics = talkTopics.substring(0,talkTopics.length-1);
    talkIds = talkIds.substring(0,talkIds.length-1);
    callback({"topics":talkTopics,"ids":talkIds});
  }

</script>